package com.xxj.duck.ui;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Enumeration;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class HttpUtil {

	private static final String actionRanking = "http://gamerank.sinaapp.com/admin/gamerank.do?method=";

	public static String getScoresTOP3() throws Exception {
		return getDataFromHttp(actionRanking + "getTOP5");
	}

	public static void submitScore(String gameName, Integer gameScore,
			String userName, String deviceId, String deviceSoftVersion,
			String phoneNumber) throws Exception {

		JSONObject myScore = null;
		try {
			myScore = HttpUtil.getGameScore(gameName, deviceId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		StringBuilder uri = new StringBuilder();
		if (null == myScore) {
			uri.append(actionRanking + "add");
		} else {
			uri.append(actionRanking + "editScore");
			uri.append("&id=").append(myScore.getInt("id"));
		}
		uri.append("&gameName=").append(gameName);
		uri.append("&gameScore=").append(gameScore);
		uri.append("&userName=").append(userName);
		uri.append("&deviceId=").append(deviceId);
		uri.append("&deviceSoftVersion=").append(deviceSoftVersion);
		uri.append("&phoneNumber=").append(phoneNumber);

		HttpResponse response = exeHttpRequest(uri.toString());
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			System.out.println("submit score OK!");
		} else {
			throw new Exception("submit score failed!");
		}
	}

	public static String queryMyRanking(String gameName, String deviceId)
			throws Exception {
		StringBuilder uri = new StringBuilder();
		uri.append(actionRanking + "getScoreRanking");
		uri.append("&gameName=").append(gameName);
		uri.append("&deviceId=").append(deviceId);
		return getDataFromHttp(uri.toString());
	}

	public static JSONObject getGameScore(String gameName, String deviceId)
			throws Exception {

		StringBuilder uri = new StringBuilder();
		uri.append(actionRanking + "getMyGameRank");
		uri.append("&gameName=").append(gameName);
		uri.append("&deviceId=").append(deviceId);
		String s = getDataFromHttp(uri.toString());
		if (s != null && !"".equals(s)) {
			JSONArray jsonArray = new JSONObject(s).getJSONArray("resultList");
			if (jsonArray.length() > 0) {
				return (JSONObject) jsonArray.opt(0);
			}
		}
		return null;
	}

	private static URI getURI(String http) {
		URI uri = null;
		try {
			uri = new URI(http);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return uri;
	}

	private static String getDataFromHttp(String http) throws Exception {
		HttpResponse response = exeHttpRequest(http);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				return EntityUtils.toString(entity, "UTF-8");
			}
		}
		return "";
	}

	private static HttpResponse exeHttpRequest(String http)
			throws ClientProtocolException, IOException {
		System.out.println("uri:" + http);
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse response;
		try {
			response = httpClient.execute(new HttpPost(getURI(http)));
		} catch (ClientProtocolException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return response;
	}

	public static void updatePlayCount(String gameName, String deviceId) {
		StringBuilder uri = new StringBuilder();
		uri.append(actionRanking + "updatePlayCount");
		uri.append("&gameName=").append(gameName);
		uri.append("&deviceId=").append(deviceId);
		try {
			exeHttpRequest(uri.toString());
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
